<template>
  <div class="login-wrap">
    <el-card class="login-card">
      <h2>登录</h2>
      <el-form :model="form" @submit.prevent>
        <el-form-item label="用户名">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password" type="password" />
        </el-form-item>
        <el-form-item label="验证码">
          <div class="captcha-container">
            <el-input 
              v-model="form.captchaCode" 
              placeholder="请输入验证码"
              class="captcha-input"
            />
            <div class="captcha-image" @click="refreshCaptcha">
              <img 
                v-if="captchaImage" 
                :src="captchaImage" 
                alt="验证码"
                class="captcha-img"
              />
              <div v-else class="captcha-placeholder">
                点击获取验证码
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doLogin" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import request from '../utils/request'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const auth = useAuthStore()
const loading = ref(false)
const captchaImage = ref('')
const captchaId = ref('')

const form = reactive({ 
  username: '', 
  password: '', 
  captchaCode: '' 
})

// 获取验证码
async function refreshCaptcha() {
  try {
    const { data } = await request.get('/api/auth/captcha')
    captchaImage.value = data.captchaImage
    captchaId.value = data.captchaId
  } catch (error) {
    ElMessage.error('获取验证码失败')
  }
}

async function doLogin() {
  if (!form.username || !form.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }
  
  if (!form.captchaCode) {
    ElMessage.warning('请输入验证码')
    return
  }

  loading.value = true
  try {
    const { data } = await request.post('/api/auth/login', {
      username: form.username,
      password: form.password,
      captchaId: captchaId.value,
      captchaCode: form.captchaCode
    })
    
    if (data.error) {
      ElMessage.error(data.error)
      // 验证码错误时刷新验证码
      if (data.error === '验证码错误') {
        refreshCaptcha()
        form.captchaCode = ''
      }
    } else {
      auth.setToken(data.token || 'mock')
      ElMessage.success('登录成功')
      router.push('/')
    }
  } catch (error) {
    ElMessage.error('登录失败')
    refreshCaptcha()
    form.captchaCode = ''
  } finally {
    loading.value = false
  }
}

// 页面加载时获取验证码
onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
.login-wrap {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card { 
  width: 400px; 
}
.login-card h2 {
  text-align: center;
  margin-bottom: 20px;
  margin-top: 0;
  color: #303133;
}
.captcha-container {
  display: flex;
  align-items: center;
  gap: 10px;
}
.captcha-input {
  flex: 1;
  min-width: 0;
}
.captcha-image {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.captcha-img {
  height: 40px;
  cursor: pointer;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
.captcha-placeholder {
  width: 120px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #909399;
  cursor: pointer;
}
.captcha-placeholder:hover {
  border-color: #409eff;
  color: #409eff;
}
</style>


